package com.yxzr.youyou.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hjq.baselibrary.base.BaseDialog;
import com.yxzr.youyou.R;
import com.yxzr.youyou.adapter.IntegralMallAdapter;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.IntegralMallBean;
import com.yxzr.youyou.bean.UserInfoBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class IntegralMallFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private IntegralMallAdapter adapter;
    private List<IntegralMallBean.DataBean> list = new ArrayList<>();
    private SharedPreferencesHelper sp;
    private UserInfoBean.DataBean userInfoBean;
    private int number = 1;
    private int integralId = 0;
    private View dialogView;
    private TextView jian,add,num;
    private BaseDialog dialog;

    public IntegralMallFragment(UserInfoBean.DataBean userInfo){
        this.userInfoBean = userInfo;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_integralmall,null);
        dialogView = inflater.inflate(R.layout.integraldialog_adapter_layout,null);
        sp = new SharedPreferencesHelper(getActivity(),"logindata");
        initView(v);
        return v;
    }

    private void initView(View v) {
        mRecyclerView = v.findViewById(R.id.recyclerView);
        jian = dialogView.findViewById(R.id.jian);
        add = dialogView.findViewById(R.id.add);
        num = dialogView.findViewById(R.id.num);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(20,1));
        adapter = new IntegralMallAdapter(getContext());
        mRecyclerView.setAdapter(adapter);

        dialog = new BaseDialog.Builder(getActivity())
                .setContentView(dialogView)
                .setWidth(800)
                .setHeight(500)
                .setOnClickListener(R.id.close, new BaseDialog.OnClickListener() {
                    @Override
                    public void onClick(BaseDialog dialog, View view) {
                        dialog.dismiss();
                    }
                })
                .setOnClickListener(R.id.shenqing, new BaseDialog.OnClickListener() {
                    @Override
                    public void onClick(BaseDialog dialog, View view) {
                        dialog.dismiss();
                        Log.e("TAG","number:"+num.getText().toString());
                        Log.e("TAG","number1:"+number);

                        Double a = Double.parseDouble(list.get(integralId).getIntegral()) * number;
                        if (Double.parseDouble(userInfoBean.getIntegral()) >= a){
                            exchange();
                        }else {
                            Toast.makeText(getContext(),"可用积分不足",Toast.LENGTH_LONG).show();
                        }

                    }
                }).create();

        //加
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number ++;
                num.setText(number+"");
            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number --;
                if (number < 1){
                    number = 1;
                }
                num.setText(number+"");
            }
        });

        adapter.setOnClickListener(new IntegralMallAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                dialog.show();
                integralId = position;
            }
        });

        initData();
    }

    private void initData(){
        RequestUtil.getInstence().API().shop((String) sp.getSharedPreference("token",""))
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        IntegralMallBean bean = gson.fromJson(t,IntegralMallBean.class);

                        list = bean.getData();
                        adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }


    private void exchange(){
        RequestUtil.getInstence().API().exchange((String) sp.getSharedPreference("token",""),list.get(integralId).getId(),num.getText().toString())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Toast.makeText(getContext(),"兑换成功",Toast.LENGTH_LONG).show();
                        initData();
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
